﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;
using Entities;

namespace QLTB_CTK34
{
    public partial class FrmUpdatePhanQuyenSuDung : Form
    {
        
        public FrmUpdatePhanQuyenSuDung()
        {
            InitializeComponent();
            this.MaximizeBox = false;
        }
        public FrmUpdatePhanQuyenSuDung(string mataikhoan):this()
        {
            txtMaTK.Text = mataikhoan;
            var YourAccount = QLTaiKhoan.FindByID(mataikhoan);
            txtTenTK.Text = YourAccount.TenDangNhap;

        }

        /// <summary>
        /// hiển thị các quyền mà tài khoản có thể sử dụng
        /// </summary>
        private void PhanQuyenSuDung_Load(object sender, EventArgs e)
        {
            var dspqsd = QLPhanQuyenSuDung.FindByCondition(x => x.MaTK == txtMaTK.Text);
            var dsqsd = QlQuyenSuDung.GetAll();
            dsqsd.ToList().ForEach(x =>
                {
                    bool check = dspqsd.Any(pqsd => pqsd.MaQuyenSuDung == x.MaQuyenSuDung);
                    clbPhanQuyenSuDung.Items.Add(x.MoTa, check);
                });
        }
        public List<PhanQuyenSuDung> dspqsd=new List<PhanQuyenSuDung>();
        /// <summary>
        ///lấy các quyền sử dụng mà được check,lưu lại
        /// </summary>
        private void btnLuu_Click(object sender, EventArgs e)
        {
            foreach (var item in clbPhanQuyenSuDung.CheckedItems)
            {
                var qsd = QlQuyenSuDung.FindByCondition(x => x.MoTa == item.ToString()).ToList()[0];
                var pqsd = new PhanQuyenSuDung();
                pqsd.MaTK = txtMaTK.Text;
                pqsd.MaQuyenSuDung = qsd.MaQuyenSuDung;
                dspqsd.Add(pqsd);
            }
            var kq = QLPhanQuyenSuDung.UpdatePhanQuyen(txtMaTK.Text, dspqsd);
            kq.Show();
            if (kq.Success)
            {
                DialogResult = DialogResult.OK;
				QLNhatKy.ThemNhatKy("Cập nhật phân quyền sử dụng");
            }
        }

        private void clbPhanQuyenSuDung_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnLuu.Enabled = true;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }


    }
}
